﻿using Chromium.Remote;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WinformMapXpSupportDemo.Forms;
using WinformMapXpSupportDemo.Model;

namespace WinformMapXpSupportDemo
{
    /// <summary>
    /// 初始化JS对象
    /// </summary>
    public partial class MainForm : Form
    {
        /// <summary>
        /// 初始化地图Js对象
        /// </summary>
        private void InitMapJsObject()
        {
            //添加Js对象
            var amapSharpObject = GaoDeMapForm.GlobalObject.AddObject("AmapSharpObject");
            //添加UpdateLngLat 方法
            amapSharpObject.AddFunction("UpdateLngLat").Execute += (sender, arg) =>
            {
                if (arg.Arguments.Length <= 0)
                {
                    return;
                }

                double lng = (double)arg.Arguments[0].DoubleValue;
                double lat = (double)arg.Arguments[1].DoubleValue;
                _AmapJsObject.UpdateLngLat(lng, lat);
            };

            //添加UpdateAddress 方法
            amapSharpObject.AddFunction("UpdateAddress").Execute += (sender, arg) =>
            {
                if (arg.Arguments.Length <= 0)
                {
                    return;
                }
                string address = arg.Arguments.GetValue(0).ToString();
                _AmapJsObject.UpdateAddress(address);
            };


            //添加ShowMessage方法
            amapSharpObject.AddFunction("ShowMessage").Execute += (sender, arg) =>
            {
                if (arg.Arguments.Length <= 0)
                {
                    return;
                }
                string content = arg.Arguments.GetValue(0).ToString();
                string caption = arg.Arguments.GetValue(1).ToString();
                string messageType = arg.Arguments.GetValue(2).ToString();
                _AmapJsObject.ShowMessage(content, caption, messageType);
            };

            //添加AddDevice方法
            amapSharpObject.AddFunction("AddDevice").Execute += (sender, arg) =>
            {
                if (arg.Arguments.Length <= 0)
                {
                    return;
                }

                double lng = (double)arg.Arguments[0].DoubleValue;
                double lat = (double)arg.Arguments[1].DoubleValue;
                //添加设备回调方法
                Action<DeviceInfo> addDeviceCallback = (d) =>
                {
                    ListViewItem newItem = new ListViewItem(d.DeviceCode + "-" + d.DeviceName, 0);
                    newItem.Tag = d;
                    listView1.Items.Add(newItem);
                    //设备添加成功后，在地图上绘制一个覆盖物logo
                    GaoDeMapForm.BrowserCore.ExecuteJavascript($"addMarker('{d.DeviceCode}',{d.DevicePosition.Longitude},{d.DevicePosition.Latitude})");
                };
                string defaultDeviceName = "设备" + listView1.Items.Count;

                _AddDeviceForm = AddDeviceForm.Create(defaultDeviceName, addDeviceCallback, new MapPosition(lng, lat));

                if(!_AddDeviceForm.Visible)
                {
                    _AddDeviceForm.Show(this);
                }
            };
            //添加GetDeviceInfo 方法
            amapSharpObject.AddFunction("GetDeviceInfo").Execute += (sender, arg) =>
            {
                if (arg.Arguments.Length <= 0)
                {
                    return;
                }

                double lng = (double)arg.Arguments[0].DoubleValue;
                double lat = (double)arg.Arguments[1].DoubleValue;
                //根据经纬度查询winfrom列表控件中对应的设备信息
                foreach (ListViewItem item in listView1.Items)
                {
                    DeviceInfo deviceInfo = item.Tag as DeviceInfo;
                    if (deviceInfo == null)
                    {
                        continue;
                    }
                    if (Math.Abs(lng - deviceInfo.DevicePosition.Longitude) <= 0.000001 && Math.Abs(lat - deviceInfo.DevicePosition.Latitude) <= 0.000001)
                    {
                        var jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(deviceInfo);
                        arg.SetReturnValue(CfrV8Value.CreateString(jsonString));
                        break;
                    }
                }
            };

            //添加ActiveDeviceInfo方法
            amapSharpObject.AddFunction("ActiveDeviceInfo").Execute += (sender, arg) => {

                if (arg.Arguments.Length <= 0)
                {
                    return;
                }
                string deviceCode = arg.Arguments[0].StringValue;
                //根据设备号查询winfrom列表控件中对应的设备信息
                foreach (ListViewItem item in listView1.Items)
                {
                    DeviceInfo deviceInfo = item.Tag as DeviceInfo;
                    if (deviceInfo == null)
                    {
                        continue;
                    }
                    if (deviceInfo.DeviceCode == deviceCode)
                    {
                        item.Selected = true;
                        this.listView1.FocusedItem = item;
                    }
                    else
                    {
                        item.Selected = false;
                        this.listView1.FocusedItem = null;
                    }

                }
                this.listView1.Focus();
            };
        }
    }
}
